import {UserActions} from './user';

const initialState = {
  products: {},
  draft: {},
  files: {},
  patientGender: 'male',
};

export const Gender = {
  male: {label: '男', value: 'male', key: 'male', inputLabel: '男'},
  female: {label: '女', value: 'female', key: 'female', inputLabel: '女'},
};

export const orderFormActions = {
  UPDATE_ORDER_FORM: 'update_order_form',
  UPDATE_ORDER_FORM_PRODUCT: 'update_order_form_product',
  REMOVE_ORDER_FORM_PRODUCT: 'remove_order_form_product',
  RESET_ORDER_FORM: 'reset_order_form',
};

export function updateOrderForm(payload) {
  return {
    type: orderFormActions.UPDATE_ORDER_FORM,
    payload,
  };
}

export function updateOrderFormProduct(payload) {
  return {
    type: orderFormActions.UPDATE_ORDER_FORM_PRODUCT,
    payload,
  };
}

export function removeOrderFormProduct(payload) {
  return {
    type: orderFormActions.REMOVE_ORDER_FORM_PRODUCT,
    payload,
  };
}

export function resetForm() {
  return {
    type: orderFormActions.RESET_ORDER_FORM,
  };
}

export default function orderFormState(state = initialState, action) {
  switch (action.type) {
    case orderFormActions.UPDATE_ORDER_FORM:
      delete action.payload.product;
      return {...state, ...action.payload};
    case orderFormActions.UPDATE_ORDER_FORM_PRODUCT:
      state.products[action.payload.pid] = action.payload;
      return {...state};
    case orderFormActions.REMOVE_ORDER_FORM_PRODUCT:
      delete state.products[action.payload];
      return {...state};
    case orderFormActions.RESET_ORDER_FORM:
      return {
        products: {},
        draft: {},
        files: {},
        patientGender: 'male',
      };
    case UserActions.USER_SIGN_OUT:
      return {
        products: {},
        draft: {},
        files: {},
        patientGender: 'male',
      };
    default:
      return state;
  }
}
